package se_adxapp

import (
	"fmt"
	"strings"
	"wx-api/adx-api/cache"
	"wx-api/adx-api/errmsg"
	"wx-api/adx-api/handlers"
	"wx-api/adx-api/model/m_adxapp"
	"wx-api/adx-api/services/comm"
	"wx-api/adx-api/utils"
)

func List(c *handlers.Controller) {
	id := c.GetString("id")
	name := c.GetString("name")
	flag := c.GetInt32("flag", 0)
	status := c.GetInt32("status", 1)
	pageNum, pageSize := c.GetPage()
	adx_uid := comm.GetUserInfo(c)
	if adx_uid == "" {
		return
	}
	data, err := m_adxapp.List(id, name, "", flag, status, pageNum, pageSize)
	if err != nil {
		c.SetErrorMsg(nil, errmsg.GetError("select list is error"))
		return
	}
	tmplist := []m_adxapp.AdxApp{}
	ttt := data.Data.(*[]m_adxapp.AdxApp)
	for _, v := range *ttt {
		tmp := v
		sinfo, ok := cache.CacheAdxListMapNGet(v.AdxId)
		if ok {
			tmp.AdxName = sinfo.Name
		}
		tmplist = append(tmplist, tmp)
	}
	data.Data = tmplist
	c.SetErrorMsg(data, errmsg.SuccessMsg())
	return
}

func Add(c *handlers.Controller) {
	var form m_adxapp.AdxApp
	if err := c.Ctx.ShouldBind(&form); err != nil {
		fmt.Println(err)
		c.BadRequest()
		return
	}
	utils.CleanStruct(&form)
	adx_uid := comm.GetUserInfo(c)
	if adx_uid == "" {
		return
	}
	if form.AdxId == "" {
		c.SetErrorMsg(nil, errmsg.GetError("所属adx is error"))
		return
	}
	adxinfo, ok := cache.CacheAdxListMapGet(form.AdxId)
	if !ok {
		c.SetErrorMsg(nil, errmsg.GetError("所属adx is error"))
		return
	}
	form.AdxId = adxinfo.Id.Hex()
	form.Code = utils.RandomString(6)
	if form.AppId == "" {
		form.AppId = utils.RandomString(6)
	}
	if form.AppName == "" {
		form.AppName = form.Name
	}
	id, err := m_adxapp.Save(form)
	if err != nil {
		c.SetErrorMsg(nil, errmsg.GetError("insert is error"))
		return
	}
	go cache.CacheAdxAppMapUpdate(id)
	c.SetErrorMsg(nil, errmsg.SuccessMsg())
	return
}

func Edit(c *handlers.Controller) {
	var form m_adxapp.AdxApp
	if err := c.Ctx.ShouldBind(&form); err != nil {
		fmt.Println(err)
		c.BadRequest()
		return
	}
	utils.CleanStruct(&form)
	adx_uid := comm.GetUserInfo(c)
	if adx_uid == "" {
		return
	}
	sspinfo, err := m_adxapp.GetByCk(form.Id.Hex(), "")
	if err != nil || sspinfo == nil {
		c.SetErrorMsg(nil, errmsg.GetError("data is empty"))
		return
	}
	err = m_adxapp.Update(form)
	if err != nil {
		c.SetErrorMsg(nil, errmsg.GetError("edit is error"))
		return
	}
	go cache.CacheAdxAppMapUpdate(sspinfo.Id.Hex())
	c.SetErrorMsg(nil, errmsg.SuccessMsg())
	return
}

func Info(c *handlers.Controller) {
	id := c.GetString("id")
	if id == "" {
		c.SetErrorMsg(nil, errmsg.GetError("必填参数为空"))
		return
	}
	adx_uid := comm.GetUserInfo(c)
	if adx_uid == "" {
		return
	}
	data, err := m_adxapp.GetByCk(id, "")
	if err != nil {
		c.SetErrorMsg(nil, errmsg.GetError("select info is error"))
		return
	}
	c.SetErrorMsg(data, errmsg.SuccessMsg())
	return
}

func Flag(c *handlers.Controller) {
	id := c.GetString("id")
	flag := c.GetInt32("flag", 0)
	if id == "" || (flag != 1 && flag != 2) {
		c.SetErrorMsg(nil, errmsg.GetError("非法参数"))
		return
	}
	adx_uid := comm.GetUserInfo(c)
	if adx_uid == "" {
		return
	}
	data, err := m_adxapp.GetByCk(id, "")
	if err != nil || data == nil {
		c.SetErrorMsg(nil, errmsg.GetError("select info is error"))
		return
	}
	err = m_adxapp.UpdateFalg(id, flag)
	if err != nil {
		c.SetErrorMsg(nil, errmsg.GetError("flag is error"))
		return
	}
	go cache.CacheAdxAppMapUpdate(id)
	c.SetErrorMsg(nil, errmsg.SuccessMsg())
	return
}

func Stats(c *handlers.Controller) {
	id := c.GetString("id")
	flag := c.GetInt32("status", 0)
	if id == "" || (flag != 1 && flag != 2) {
		c.SetErrorMsg(nil, errmsg.GetError("非法参数"))
		return
	}
	adx_uid := comm.GetUserInfo(c)
	if adx_uid == "" {
		return
	}
	data, err := m_adxapp.GetByCk(id, "")
	if err != nil || data == nil {
		c.SetErrorMsg(nil, errmsg.GetError("select info is error"))
		return
	}
	err = m_adxapp.UpdateDel(id, flag)
	if err != nil {
		c.SetErrorMsg(nil, errmsg.GetError("stats is error"))
		return
	}
	go cache.CacheAdxAppMapUpdate(id)
	c.SetErrorMsg(nil, errmsg.SuccessMsg())
	return
}

// 获取所有所属应用
func All(c *handlers.Controller) {
	adx_uid := comm.GetUserInfo(c)
	if adx_uid == "" {
		c.SetErrorMsg(nil, errmsg.GetError("无缓存，请联系管理员"))
		return
	}
	adx_id := c.GetString("adx_id")
	if adx_id == "" {
		c.SetErrorMsg(nil, errmsg.GetError("无ADX缓存，请联系管理员"))
		return
	}
	adx_ids := strings.Split(adx_id, ",")
	appids := []string{}
	for _, id := range adx_ids {
		tmp_ids := cache.CacheAdxAppResAdxUidGet(id)
		appids = append(appids, tmp_ids...)
	}
	app_arr := []m_adxapp.AdxApp{}
	for _, id := range appids {
		info, ok := cache.CacheAdxAppMapGet(id)
		if !ok {
			continue
		}
		app_arr = append(app_arr, m_adxapp.AdxApp{
			Id:          info.Id,
			Code:        info.Code,
			Name:        info.Name,
			AppId:       info.AppId,
			AppName:     info.AppName,
			MediaType:   info.MediaType,
			Description: info.Description,
		})
	}
	c.SetErrorMsg(app_arr, errmsg.SuccessMsg())
	return
}
